/**
 * Created by lanouhn on 17/7/18.
 */
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.*;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class GetUrlPic {
    public static void main(String[] args) throws Exception{
        URL url=new URL("http://t1.niutuku.com/960/24/24-617747.jpg");
        HttpURLConnection conn=(HttpURLConnection)url.openConnection();
        conn.setRequestMethod("GET");
        conn.setConnectTimeout(5*1000);
        InputStream inStream=conn.getInputStream();
        byte[] data= readInputStream(inStream);
        File imageFile=new File("/Users/lanouhn/Desktop/result1");
        FileOutputStream outStream=new FileOutputStream(imageFile);
        outStream.write(data);
        outStream.close();

    }
    public static byte[] readInputStream(InputStream inStream) throws Exception{
        ByteArrayOutputStream outStream= new ByteArrayOutputStream();
        byte[] buffer=new byte[1024];
        int len=0;
        while((len=inStream.read(buffer))!=-1){
            outStream.write(buffer,0,len);
        }
        inStream.close();
        return outStream.toByteArray();
    }
}
